package com.bootdo.clouddo.service.user.impl;

import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.bootdo.clouddo.dao.user.QaUsersMapper;
import com.bootdo.clouddo.domain.user.QaUsers;
import com.bootdo.clouddo.service.user.IUserService;
import com.bootdo.clouddo.redis.CommonRedisService;
import com.bootdo.clouddo.redis.RedisEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.Optional;

@Service
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private QaUsersMapper dao;

	@Autowired
	private CommonRedisService redisService;

//	@Autowired
//	private IQaUsersLevelService levelService;

	@Override
	public QaUsers findById(Long uid) {
		QaUsers user =  dao.selectByPrimaryKey(uid);
		if(user == null)
			return null;
		//levelService.setLevelToUser(user);
		// 计算签到天数统计
		//user.setSignInDaysRank(findSignInDaysRank(user.getUid()));
		countOverPrec(user);
		return user;
	}

	@Override
	public Double getRawExp(Long uid) {
		QaUsers user =  dao.selectByPrimaryKey(uid);
		if(user == null)
			return 0d;
		return user.getExp();
	}

	@Override
	public QaUsers selectByUserName(String userName) {
		return dao.selectByUserName(userName);
	}

	@Override
	public Integer updateUserInfoById(QaUsers qaUsers) {
		return dao.updateUserInfoById(qaUsers);
	}

	@Override
	public QaUsers findByMobile(String mobile) {
		return dao.selectByMobile(mobile);
	}

	@Override
	public boolean existsByMobile(String mobile) {
		return dao.existsByMobile(mobile);
	}

	@Override
	public int save(QaUsers appUser) {
		return dao.insertSelective(appUser);
	}

	@Override
	public Integer updatePwdByMobile(QaUsers user) {
		return dao.updatePwdByMobile(user);
	}

	@Override
	public QaUsers selectByUserNameOrMobile(String name) {
		return dao.selectByUserNameOrMobile(name);
	}

	@Override
	public Integer modifyPasswordById(QaUsers user) {
		return dao.modifyPasswordById(user);
	}

	@Override
	public int updateAvatar(Long uid, String url) {
		return dao.updateAvatar(uid, url);
	}

	@Override
	@Async
	public int fieldIncrease(String field, Integer val, Long uid) {
		return dao.fieldIncrease(field, val, uid);
	}

	@Override
	public QaUsers selectByWxOpenId(String openId) {
		return dao.selectByWxOpenId(openId);
	}


	@Override
	public int updateUserWxInfoById(QaUsers user, WxMaUserInfo wxUserInfo) {
		// 重新new对象，防止属性被覆盖
		QaUsers _user = new QaUsers(user.getUid());
		buildWxUserInfo(_user, wxUserInfo);
		return dao.updateUserInfoById(_user);
	}

	@Override
	public int updateUserMobileoById(QaUsers user, WxMaPhoneNumberInfo phontInfo) {
		QaUsers _user = new QaUsers(user.getUid());
		_user.setMobile(phontInfo.getPhoneNumber());
		return dao.updateUserMobileById(_user);
	}

	@Override
	public int updateVip(Long uid) {
		return dao.updateVip(uid);
	}

	@Override
	public int updateIntegralExp(Double obtainIntegral, Double obtainExp, Long uid) {
		return dao.updateIntegralExp(obtainIntegral, obtainExp, uid);
	}

	private QaUsers buildWxUserInfo(QaUsers user, WxMaUserInfo wxUserInfo){
		user.setNickname(wxUserInfo.getNickName());
		user.setUserName(wxUserInfo.getNickName());
		user.setAvatarFile(wxUserInfo.getAvatarUrl());
		user.setSex(wxUserInfo.getGender());
		user.setCity(wxUserInfo.getCity());
		user.setProvince(wxUserInfo.getProvince());
		user.setWxUnionid(wxUserInfo.getUnionId());
		return user;
	}

	@Override
	public boolean checkVip(QaUsers user) {
		if(user.getVip() == null || !user.getVip()) {
			// 再次确认，判断是否是会员
			user = findById(user.getUid());
			// 更新redis用户缓存
			if(user.getVip() == null || !user.getVip())
				return false;
		}
		return true;
	}

	@Override
	public Long countTotal() {
		Long totalUser = -1L;
//		Object obj = redisService.get(RedisEnum.STATISTICS_DAILY.getKey());
//		if(obj instanceof QaStatisticsDaily)
//			totalUser = ((QaStatisticsDaily) obj).getTotalUserCount();
		//totalUser = dao.countTotal();
		return totalUser;
	}

	private void countOverPrec(QaUsers user){
		Long total = countTotal();
		if(total <= 0)
			return;
		NumberFormat nf = NumberFormat.getNumberInstance();
		// 保留两位小数
		nf.setMaximumFractionDigits(2);
	}

	@Override
	public Integer findExpRank(Long uid) {
		Optional<Integer> op = redisService.hGet(RedisEnum.RANK_EXP_ALL.getKey(), String.valueOf(uid));
		if(op.isPresent())
			return op.get();
		return 0;
	}

	@Override
	public Integer findSignInDaysRank(Long uid) {
		Optional<Integer> op = redisService.hGet(RedisEnum.RANK_SIGN_IN_DAYS_ALL.getKey(), String.valueOf(uid));
		if(op.isPresent())
			return op.get();
		return 0;
	}

	@Override
	public Integer findAgreeCountRank(Long uid) {
		Optional<Integer> op = redisService.hGet(RedisEnum.RANK_AGREE_COUNT_ALL.getKey(), String.valueOf(uid));
		if(op.isPresent())
			return op.get();
		return 0;
	}

	@Override
	public Double findNewIntegral(Long uid) {
		return dao.findNewIntegral(uid);
	}

	@Override
	public Double findNewExp(Long uid) {
		return dao.findNewExp(uid);
	}

}
